//
//  CollectViewController.m
//  FruitsAndHealthy
//
//  Created by appying_wxj on 14/11/20.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectContentViewController.h"
@interface CollectViewController ()

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLab.text = @"收藏";
    self.view.backgroundColor = [UIColor colorWithRed:1 green:0.73 blue:0.29 alpha:1];
     [self.leftButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
     [self CollectInitUI];
    [BaseFruits createTable];
    self.array = [BaseFruits getAllFruitsTitle];
    if ([self.array count] == 0) {
        [self reminderUI];
    }else{
        [self CollectInitUI];
    }
    
//    NSLog(@"已经收藏的数据有%@",self.array);
   
}
-(void)reminderUI{
    UIImageView *reminderImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, kScreen_Width-40, kScreen_Height-200)];
    reminderImage.backgroundColor = [UIColor clearColor];
    reminderImage.image = [UIImage imageNamed:@"image_collect@2x"];
    [self.view addSubview:reminderImage];
}
// 界面
-(void)CollectInitUI{
    
    _collectTableView = [[UITableView alloc] init];
    _collectTableView.frame = CGRectMake(0, 64, kScreen_Width, kScreen_Height-64);
    _collectTableView.delegate = self;
    _collectTableView.dataSource = self;
    _collectTableView.rowHeight = 60;
//    _collectTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _collectTableView.separatorStyle = NO;// 隐藏cell上的分割线
    [self.view addSubview:_collectTableView];
    
}
#pragma mark------UITableViewDataSource------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    }
    else{
        while ([cell.contentView.subviews lastObject]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // 分割的线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 59.5, kScreen_Width-20, 0.5)];
    line.backgroundColor = [UIColor colorWithRed:0.38 green:0.34 blue:0.3 alpha:0.5];
    [cell.contentView addSubview:line];
    
    // 单元格将要显示的时候 ，跟句单元格的索引值从数组中取出相应的people对象
    Fruits *p = [_array objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",p.fruitsTitle];
    cell.textLabel.textColor = [UIColor colorWithRed:0.38 green:0.34 blue:0.3 alpha:1];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
//    cell.textLabel.text = [_array objectAtIndex:indexPath.row];
    
    
    return cell;
}
//点击单元格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    dispatch_async(dispatch_get_main_queue(), ^
                   {
                       CollectContentViewController *content = [[CollectContentViewController alloc] init];
                       content.fruitsName = [[_array objectAtIndex:indexPath.row] fruitsTitle];
                       content.fruitsContent = [[_array objectAtIndex:indexPath.row] fruitsContent];
                       [self presentViewController:content animated:YES completion:nil];
                       
                   });
}
// 编辑单元格  删除数据库中的数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 1,根据索引从数组中找到要删除的对象
    Fruits *p = [_array objectAtIndex:indexPath.row];
    
    [BaseFruits deleteFruits:p.fruitsTitle];
    
    // 3.删除数组中的对象
    [_array removeObjectAtIndex:indexPath.row];
    
    // 4.删除行
    [_collectTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    
    self.array = [BaseFruits getAllFruitsTitle];
    [_collectTableView reloadData];
}
#pragma mark-------UIButton Click--------
-(void)backButtonClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
